//
//  RefuseAlertView.m
//  TengyunStation
//
//  Created by Johnny on 15/10/28.
//  Copyright © 2015年 xingrong. All rights reserved.
//

#import "RefuseAlertView.h"
#import "TSHeader.h"

#define AlertWidth          272.0
#define AlertHeight         165.0

#define TextViewLength      100

@implementation RefuseAlertView

- (id)initWithResuseAlertViewDelegate:(id<RefuseAlertViewDelegate>)delegate {
    CGSize size = [UIScreen mainScreen].bounds.size;
    self = [super initWithFrame:CGRectMake((size.width - AlertWidth)/2.0, 30, AlertWidth, AlertHeight)];
    if (self) {
        _delegate = delegate;
        self.layer.cornerRadius = 4;
        self.clipsToBounds = YES;
        self.backgroundColor = UIColorFromRGB(0xfbfbfb);
        _tipLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 15, AlertWidth - 30, 15)];
        _tipLabel.backgroundColor = [UIColor clearColor];
        _tipLabel.font = [UIFont systemFontOfSize:14];
        _tipLabel.textColor = TipTextColor;
        _tipLabel.text = @"添加拒收理由（非必填）";
        [self addSubview:_tipLabel];
        _contentTextView = [[UITextView alloc] initWithFrame:CGRectMake(12, 6, AlertWidth - 24, AlertHeight - 44 - 6 - 15)];
        _contentTextView.backgroundColor = [UIColor clearColor];
        _contentTextView.font = [UIFont systemFontOfSize:14];
        _contentTextView.textColor = MainTextColor;
        _contentTextView.delegate = self;
        [self addSubview:_contentTextView];
        
        _numberLabel = [[UILabel alloc] initWithFrame:CGRectMake(AlertWidth - 100 - 15, AlertHeight - 44 - 15, 100, 15)];
        _numberLabel.backgroundColor = [UIColor clearColor];
        _numberLabel.font = [UIFont systemFontOfSize:14];
        _numberLabel.textColor = TipTextColor;
        _numberLabel.textAlignment = NSTextAlignmentRight;
        _numberLabel.text = @"0/100";
        [self addSubview:_numberLabel];
        [self loadBottomButton];
    }
    return self;
}

- (void)loadBottomButton {
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelBtn.frame = CGRectMake(0, AlertHeight - 44, AlertWidth/2.0, 44);
    cancelBtn.backgroundColor = [UIColor clearColor];
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:MainTextColor forState:UIControlStateNormal];
    cancelBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    cancelBtn.tag = 100;
    cancelBtn.layer.borderColor = LineColor.CGColor;
    cancelBtn.layer.borderWidth = 0.5;
    [cancelBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:cancelBtn];
    UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    sureBtn.frame = CGRectMake(AlertWidth/2.0, AlertHeight - 44, AlertWidth/2.0, 44);
    sureBtn.backgroundColor = [UIColor clearColor];
    [sureBtn setTitle:@"确认" forState:UIControlStateNormal];
    [sureBtn setTitleColor:OrangeColor forState:UIControlStateNormal];
    sureBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    sureBtn.tag = 101;
    sureBtn.layer.borderColor = LineColor.CGColor;
    sureBtn.layer.borderWidth = 0.5;
    [sureBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:sureBtn];
}

- (void)buttonAction:(UIButton *)button {
    if (button.tag == 100) {
        [self removeReceiverAlertView];
    } else if (button.tag == 101) {
        [_contentTextView resignFirstResponder];
        if ([_delegate respondsToSelector:@selector(refuseReceiveExpressReason:)]) {
            [_delegate refuseReceiveExpressReason:_contentTextView.text];
        }
        [self removeReceiverAlertView];
    }
}

//移除alertWindow
- (void)removeReceiverAlertView {
    [self endEditing:YES];
    [_alertWindow resignFirstResponder];
    [_alertWindow resignKeyWindow];
    _alertWindow.alpha = 0;
    _alertWindow = nil;
}

- (void)show {
    if (!_alertWindow) {
        _alertWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
        _alertWindow.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
        _alertWindow.windowLevel = UIWindowLevelNormal;
    }
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showKeyboard:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideKeyboard:) name:UIKeyboardWillHideNotification object:nil];
    [_contentTextView becomeFirstResponder];
    [_alertWindow addSubview:self];
    [_alertWindow makeKeyAndVisible];
}

#pragma mark Notification
- (void)showKeyboard:(NSNotification *)notification {
    [UIView animateWithDuration:0.25 animations:^{
        self.frame = CGRectMake(self.frame.origin.x, 30, self.frame.size.width, self.frame.size.height);
    }];
}

- (void)hideKeyboard:(NSNotification *)notification {
    [UIView animateWithDuration:0.25 animations:^{
        self.frame = CGRectMake(([UIScreen mainScreen].bounds.size.width - self.frame.size.width)/2.0, ([UIScreen mainScreen].bounds.size.height - self.frame.size.height)/2.0, self.frame.size.width, self.frame.size.height);
    }];
}

#pragma mark UITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView {
    if (textView.text.length <= 0) {
        _tipLabel.hidden = NO;
    } else {
        _tipLabel.hidden = YES;
    }
    UITextRange *selectedRange = [textView markedTextRange];
    UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
    if (!position) {
        [self textViewText:textView.text];
    }
}

- (void)textViewText:(NSString *)text {
    if (text.length > TextViewLength) {
        _contentTextView.text = [text substringToIndex:TextViewLength];
        [_contentTextView resignFirstResponder];
    }
    _numberLabel.text = [NSString stringWithFormat:@"%d/%d",(int)_contentTextView.text.length,TextViewLength];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
@end
